<template>
  <div class="home">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card class="mt16">
      <!-- 搜索框 -->
      <el-input
        placeholder="请输入你想要搜索的内容"
        v-model="shuju.query"
        :clearable="true"
        @clearable="qksearch"
        @input="qksearch"
      >
        <i slot="append" class="el-icon-search" @click="gjsearch"></i>
      </el-input>
      <!-- 搜索框 -->
      <!-- 添加按钮 -->
      <el-button
        type="primary"
        style="margin-left: 15px"
        @click="dialogFormVisible = true"
        >添加用户</el-button
      >
      <!-- 添加按钮 -->
      <!-- 表格区 -->
      <el-table :data="user" border style="width: 100%" class="mt16">
        <el-table-column fixed label="#" type="index" style="width: 10px">
        </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column prop="zip" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeszt(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="edit(scope.row)"
              type="primary"
              size="small"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            ></el-button>
            <el-button
              type="warning"
              size="small"
              icon="el-icon-setting"
              @click="xurole(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格区 -->
      <!-- 模态框 -->
      <el-dialog
        title="添加用户"
        :visible.sync="dialogFormVisible"
        @close="close"
      >
        <el-form
          :model="form"
          :rules="rules"
          ref="users"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" style="width: 100%"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="form.password"
              style="width: 100%"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" style="width: 100%"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="mobile">
            <el-input v-model="form.mobile" style="width: 100%"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">添加</el-button>
        </div>
      </el-dialog>
      <!-- 模态框 -->
      <!-- 编辑的模态框 -->
      <el-dialog
        title="编辑用户"
        :visible.sync="dialogFormVisible1"
        @close="close"
      >
        <el-form
          :model="form1"
          :rules="rules"
          ref="edusers"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="form1.username"
              style="width: 100%"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form1.email" style="width: 100%"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="mobile">
            <el-input v-model="form1.mobile" style="width: 100%"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="xg">确定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑的模态框 -->
      <!-- 分页器 -->
      <el-pagination
        @size-change="changesize"
        @current-change="changepagenum"
        :current-page="shuju.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="shuju.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 分页器 -->
      <!-- 设置的模态框 -->
      <el-dialog title="分配角色" :visible.sync="dialogVisiblesz" width="30%">
        <div class="fp">当前的用户: {{ options.username }}</div>
        <div style="margin: 30px 0">当前的角色: {{ options.role_name }}</div>
        <div>
          <span style="margin-right: 15px">分配新角色</span>
          <el-select v-model="options.id" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisiblesz = false">取 消</el-button>
          <el-button type="primary" @click="xurole1">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 设置的模态框 -->
    </el-card>
  </div>
</template>

<script>
import { userlist } from '@/http/api.js'
import { adduser } from '@/http/api.js'
import { delmore } from '@/http/api.js'
import { edituser } from '@/http/api.js'
import { changestatus } from '@/http/api.js'
import { getfpjs, userroles } from '@/http/api.js'
import _ from 'lodash'
export default {
  data() {
    return {
      user: [], //用户的数据
      total: 0,
      shuju: {
        query: '', //搜索框绑定的字段
        pagenum: 1,
        pagesize: 5
      },
      dialogVisiblesz: false, //设置的模态框
      dialogFormVisible: false, //用于绑定添加模态框的变量
      dialogFormVisible1: false, //用于绑定编辑模态框的变量
      form: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      form1: {
        username: '',
        email: '',
        mobile: '',
        id: 0
      },
      //   改变状态时用的
      changezt1: {
        id: 0,
        mg_state: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入用户邮箱', trigger: 'blur' },
          {
            pattern: /^\w+@[A-z0-9]{2,5}\.[a-z]{2,3}$/,
            message: '请正确输入邮箱格式',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '请输入用户手机号', trigger: 'blur' },
          {
            pattern: /^1[3-9][0-9]{9}$/,
            message: '请正确输入邮箱格式',
            trigger: 'blur'
          }
        ]
      },
      // 分配权限
      role: {
        id: 0
      },
      options: [] //下拉框里得值
    }
  },
  mounted() {
    this.getlist()
  },
  methods: {
    //   获取列表数据
    async getlist() {
      const res = await userlist(this.shuju)
      console.log('列表数据', res)
      this.user = res.users
      this.total = res.total
    },
    // 添加用户
    add() {
      this.$refs['users'].validate(async (valid) => {
        if (valid) {
          const res = await adduser(this.form)
          console.log('addusers', res)
          // this.role_id = res.role_id
          this.user = res
          this.getlist()
          this.dialogFormVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 清除输入框的内容
    close() {
      this.form = {
        username: '',
        password: '',
        email: '',
        mobile: ''
      }
    },
    // 删除的方法
    del(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delmore(id)
          console.log('删除', res)
          this.getlist()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 改变一页有多少条数
    changesize(val) {
      console.log(val)
      this.shuju.pagesize = val
      this.getlist()
    },
    // 改变当前页码数
    changepagenum(val) {
      console.log(val)
      this.shuju.pagenum = val
      this.getlist()
    },
    // 编辑的方法  数据回填
    edit(item) {
      console.log(item)
      this.dialogFormVisible1 = true
      this.form1.username = item.username
      this.form1.email = item.email
      this.form1.mobile = item.mobile
      this.form1.id = item.id
    },
    // 编辑用户
    async xg() {
      this.$refs['edusers'].validate(async (valid) => {
        if (valid) {
          const res = await edituser(this.form1)
          console.log('编辑', this.form1)
          console.log(res)
          this.getlist()
          this.dialogFormVisible1 = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 改变状态
    async changeszt(val) {
      console.log(val)
      let { id, mg_state } = val
      this.changezt1.id = id
      this.changezt1.mg_state = mg_state
      const res = await changestatus(this.changezt1)
      console.log('改变状态', res)
    },
    // 修改角色
    async xurole(scope) {
      this.dialogVisiblesz = true
      // 获取权限的数据
      let res = await userroles()
      this.options = res
      console.log('这是用户模块的角色', res)
      console.log('这是用户模块的角色scope', scope)
      this.role.id = scope.id //当前自己的id
    },
    async xurole1() {
      this.dialogVisiblesz = false
      const { id } = this.options //获取角色权限的id
      console.log('提取的ID', id)
      const res = await getfpjs(this.role.id, id)
      console.log('修改角色', res)
      this.getlist()
    },
    // 当输入框清空的时候触发的方法
    qksearch() {
      this.getlist()
    },
    // 搜索的方法
    gjsearch: _.debounce(function () {
      this.getlist()
    }, 300)
  }
}
</script>
<style lang="scss" scoped>
.home {
  width: 100%;
  .el-card {
    .el-input {
      width: 30%;
    }
  }
}
</style>
